<?php
class ControllerModuleCatalogLatest extends Controller {
	var $error = array();
	 
	function index() { 
		$database	=& $this->locator->get('database');
		$language	=& $this->locator->get('language');
		$module		=& $this->locator->get('module');
		$request	=& $this->locator->get('request');
		$response	=& $this->locator->get('response');
		$template	=& $this->locator->get('template');
		$session	=& $this->locator->get('session');
		$url		=& $this->locator->get('url');
		
		$language->load('controller/module_catalog_latest.php');

		$template->set('title', $language->get('heading_title'));
				
		if ($request->isPost() && $request->has('latest_status', 'post') && $this->validate()) {
			
			$database->query("delete from setting where type = 'catalog' and `group` = 'latest'");
			
			foreach ($_POST as $key => $value) {
				$database->query("insert into setting set type = 'catalog', `group` = 'latest', `key` = '".$key."', `value` = '".$value."'");
			}
			
			$session->set('message', $language->get('text_message'));
			
			$response->redirect($url->ssl('extension', FALSE, array('type' => 'module')));
		}
		
		$view = $this->locator->create('template');

		$view->set('tpl', $template);
		
		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));		

		$view->set('text_enabled', $language->get('text_enabled'));
		$view->set('text_disabled', $language->get('text_disabled'));
		$view->set('text_yes', $language->get('text_yes'));
		$view->set('text_no', $language->get('text_no'));

		$view->set('entry_columnizer', $language->get('entry_columnizer'));
		$view->set('entry_image_size', $language->get('entry_image_size'));
		$view->set('entry_image_width', $language->get('entry_image_width'));
		$view->set('entry_image_height', $language->get('entry_image_height'));
		$view->set('entry_status', $language->get('entry_status'));
		$view->set('entry_cpr', $language->get('entry_cpr'));
		$view->set('entry_limit', $language->get('entry_limit'));
		$view->set('entry_atc', $language->get('entry_atc'));
		$view->set('entry_max_desc', $language->get('entry_max_desc'));
		
		$view->set('help_cpr', $language->get('help_cpr'));
		$view->set('help_max_desc', $language->get('help_max_desc'));
		
		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('tab_general', $language->get('tab_general'));
		$view->set('tab_columnizer', $language->get('tab_columnizer'));

		$view->set('error', @$this->error['message']);

		$view->set('action', $url->ssl('module_catalog_latest'));
		
		$view->set('cancel', $url->ssl('extension', FALSE, array('type' => 'module')));	

		$results = $database->getRows("select * from setting where type = 'catalog' and `group` = 'latest'");
		
		foreach ($results as $result) {
			$name = $result['key'];
			$view->set($name, $request->has($name, 'post')?$request->gethtml($name, 'post'):$result['value']);
		}
							
		$template->set('content', $view->fetch('content/module_catalog_latest.tpl'));

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}
	
	function validate() {
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');

		if (!$user->hasPermission('modify', 'module_catalog_latest')) {
			$this->error['message'] = $language->get('error_permission');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
	
	function install() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		$user     =& $this->locator->get('user');
		
		$language->load('controller/module_catalog_latest.php');
		
		if ($user->hasPermission('modify', 'module_catalog_latest')) {
			$database->query("delete from setting where type = 'catalog' and `group` = 'latest'");
			
			$database->query("insert into setting set type = 'catalog', `group` = 'latest', `key` = 'latest_status', value = '0'");
			//$database->query("insert into setting set type = 'catalog', `group` = 'latest', `key` = 'latest_max_cols', value = '3'");
			//$database->query("insert into setting set type = 'catalog', `group` = 'latest', `key` = 'latest_limit', value = '6'");
			
			$session->set('message', $language->get('text_message'));
		} else {
			$session->set('error', $language->get('error_permission'));
		}	
				
		$response->redirect($url->ssl('extension', FALSE, array('type' => 'module')));	
	}
	
	function uninstall() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		$user     =& $this->locator->get('user');
		
		$language->load('controller/module_catalog_latest.php');
		
		if ($user->hasPermission('modify', 'module_catalog_latest')) {
			$database->query("delete from setting where type = 'catalog' and `group` = 'latest'");
			
			$session->set('message', $language->get('text_message'));
		} else {
			$session->set('error', $language->get('error_permission'));
		}	
				
		$response->redirect($url->ssl('extension', FALSE, array('type' => 'module')));
	}
}
?>
